<?php
/**
 * Created by PhpStorm.
 * User: chenxiongde
 * Date: 17/4/30
 * Time: 下午3:20
 */
final  class Application{
    public  static function  run(){
        self::_init();
        self::_set_url();
        spl_autoload_register(array(__CLASS__,'_autoload'));
        self::_create_demo();
        self::_runit();
    }
    //初始化框架
    private static  function  _init(){
        config(include MANGO_CONF_PATH.'/config.php');
        $userConf =APP_CONF_PATH.'/config.php';
        $str = <<<STR
<?php
    //配置项=>配置值
    return array(

    );
?>
STR;
        is_file($userConf) || file_put_contents($userConf,$str);
        config(include $userConf);
        // 设置session开关
        config('SESSION_AUTO_START') && session_start();
        //设置时区
        date_default_timezone_set(config('DEFAULT_TIMEZONE'));

    }
    //设置外部路径

    private static function  _set_url(){
        $path = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/'.$_SERVER['SCRIPT_NAME'];
        define('__APP__',$path);
        define('__ROOT__',dirname($path));
        define('__PUBLIC__',__ROOT__.'/'.APP_NAME.'/Public');

    }

    /**
     * 自动加载
     */
    private static function  _autoload($className){
        include APP_CONTROLLER_PATH.'/'.$className.'.php';
    }

    /**
     * 创建demo
     */
    private  static  function  _create_demo(){
        $path = APP_CONTROLLER_PATH.'/IndexController.php';
        $str = <<<STR
<?php
    class IndexController  extends Controller{
        public  function   index(){
            echo   '<h1  style="width:100%;margin:240px 35%;">欢迎使用MANGO框架</h1>';
        }
    }
?>
STR;
        is_file($path) || file_put_contents($path,$str);

    }

    /**
     * 运行框架
     */
    private static function  _runit(){
        $c = isset($_GET[config('VAR_CONTROLLER')])?config('VAR_CONTROLLER'):'Index';
        $c.='Controller';
        $a = isset($_GET[config('VAR_ACTION')])?config('VAR_ACTION'):'index';
        $obj = new $c();
        $obj->$a();
    }
}
?>